package org.hibernate.tutorial;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.tutorial.domain.Event;
import org.hibernate.tutorial.util.HibernateUtil;

import com.play.hibernate.model.Frage;
import com.play.hibernate.model.User;

public class EventManager {

    public static void main(String[] args) {
        EventManager mgr = new EventManager();

        if (args[0].equals("store")) {
//            mgr.createAndStoreEvent("My Event", new Date());
//            mgr.createUser();
            mgr.testQuery();
        } else if (args[0].equals("list")) {
            List events = mgr.listEvents();
            for (int i = 0; i < events.size(); i++) {
                Event theEvent = (Event) events.get(i);
                System.out.println(
                        "Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate()
                );
            }
        }

        HibernateUtil.getSessionFactory().close();
    }
    
    private void testQuery() {
    	
    	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
    	session.beginTransaction();
		Query query = session.getNamedQuery("findUserById").setString("id", "2");
		//Erstes Element aus der Resultliste ist der Benutzer,
		//da K�rzel eindeutig/einmalig
		List<User> usrs = query.list();
		for (User usr : usrs) {
			System.out.println(usr.getNachname());
		}
		session.getTransaction().commit();
    }
        
        private void createUser() {
        	Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
            User user = new User();
            user.setEmail("robinnnn@gg.com");
            user.setKuerzel("BOBo");
            user.setLehrer(false);
            user.setVorname("Robimmmng");
            user.setNachname("Kirmmmbachg");
            user.setPasswort("blummmmmbggg");
		    session.save(user);
		    Frage frage = new Frage();
		    frage.setFrage("TEST");
//		    frage.setOwner(user);
		    frage.setTyp("HUH");
		    frage.setUser(user);
		    session.save(frage);
		    session.getTransaction().commit();
	}

		private List listEvents() {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            session.beginTransaction();
//            List result = session.createQuery("from Event").list();
//            User user = session.createQuery("from user").;
            session.getTransaction().commit();
            return null;
        }

    private void createAndStoreEvent(String title, Date theDate) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Event theEvent = new Event();
        theEvent.setTitle(title);
        theEvent.setDate(theDate);
        session.save(theEvent);

        session.getTransaction().commit();
    }

}